public class ImprovedPrimePrinter {
    
    
    
    public static void sievePrimes(int limit) {
        boolean[] isPrime = new boolean[limit + 1];
        
 
        for (int i = 2; i <= limit; i++) {
            isPrime[i] = true;
        }
        
       
        for (int i = 2; i * i <= limit; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= limit; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        
    
        int count = 0;
        for (int i = 2; i <= limit; i++) {
            if (isPrime[i]) {
                System.out.printf("%-6d", i);
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
        
        if (count % 5 != 0) {
            System.out.println();
        }
        System.out.println("total " + count);
    }
    
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        sievePrimes(20000);
        long endTime = System.currentTimeMillis();
        System.out.println("cost " + (endTime - startTime));
    }
}